package pers.cz.json;

import com.google.gson.*;
import org.springframework.beans.BeanUtils;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * Json字符串工具类
 * @program: PostGirl-panent
 * @description: JsonUtil
 * @author: Cheng Zhi
 * @create: 2021-03-15 15:19
 **/
public class JsonUtil {

    private static Gson gson;

    static {
        gson = new Gson();
    }
    /**
     * 递归替换json中的指定key的value
     * @param jsonElement
     * @param map
     * @return
     */
    public static JsonElement replaceJsonNode(JsonElement jsonElement, Map<String,String> map) {

        if (map.size() == 0) {
            return jsonElement;
        }

        // 判断如果是简单json串直接返回
        if (jsonElement.isJsonPrimitive()) {
            return jsonElement;
        }

        // 判断如果是数组类型字符串，则逐个解析
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            JsonArray jsonArryNew = new JsonArray();
            Iterator iterator = jsonArray.iterator();
            while (iterator.hasNext()) {
                // 递归调用
                jsonArryNew.add(replaceJsonNode((JsonElement) iterator.next(),map));
            }
            return jsonArryNew;
        }

        // 判断如果是key-value类型的
        if (jsonElement.isJsonObject()) {
            JsonObject object = jsonElement.getAsJsonObject();
            JsonObject objectNew = new JsonObject();
            Iterator iterator = object.keySet().iterator();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                // 如果和目标字段匹配则更换value;
                if (map.keySet().contains(key)) {
                    String newValue = map.get(key);
                    object.addProperty(key,newValue);
                }
                JsonElement jsonEle = object.get(key);
                JsonElement jsonElementNew = replaceJsonNode(jsonEle, map);
                objectNew.add(key,jsonElementNew);
            }
            return  objectNew;
        }
        return jsonElement;
    }

    /**
     * 对象序列化
     * @param object
     * @return
     */
    public static String serialize(Object object) {

        String json = gson.toJson(object);
        return json;
    }

    /**
     * 提取并格式化账管报文为请求模板
     * @param jsonStr
     * @return
     */
    public static String anaylizeAmsJson(String jsonStr) {

        JsonElement jsonEle = new JsonParser().parse(jsonStr);
        JsonObject jsonAll = jsonEle.getAsJsonObject();
        jsonAll.remove("sOper"); // 移除"sOper"节点
        JsonObject old = jsonAll.deepCopy();
        // 将剩余字符串反序列化为对象
        JsonElement jsonElement = replaceJsonNode(jsonAll);
        System.out.println(old.toString());
        return jsonElement.toString();
    }

    /**
     * 将json字符串上的所有value替换为指定值
     * @param jsonElement
     * @return
     */
    public static JsonElement replaceJsonNode(JsonElement jsonElement) {

        // 判断如果是简单json串直接返回
        if (jsonElement.isJsonPrimitive()) {
            return jsonElement;
        }

        // 判断如果是数组类型字符串，则逐个解析
        if (jsonElement.isJsonArray()) {
            JsonArray jsonArray = jsonElement.getAsJsonArray();
            JsonArray jsonArryNew = new JsonArray();
            Iterator iterator = jsonArray.iterator();
            while (iterator.hasNext()) {
                // 递归调用
                jsonArryNew.add(replaceJsonNode((JsonElement) iterator.next()));
            }
            return jsonArryNew;
        }

        // 判断如果是key-value类型的
        if (jsonElement.isJsonObject()) {
            JsonObject object = jsonElement.getAsJsonObject();
            JsonObject objectNew = new JsonObject();
            Iterator iterator = object.keySet().iterator();
            while (iterator.hasNext()) {
                String key = (String) iterator.next();
                JsonElement jsonEle = object.get(key);
                if (jsonEle.isJsonPrimitive()) {
                    // 如果和目标字段匹配则更换value;
                    String newValue = "${" + key + "}";
                    object.addProperty(key,newValue);
                }
                JsonElement jsonElementNew = replaceJsonNode(jsonEle);

            }
            return  object;
        }
        return jsonElement;
    }


    public static void main(String[] args) {
        //JsonUtil json = new JsonUtil();
        //String str = "{\"data\":{\"pocketExtOut\":{\"listPocketExtDetail\":[{\"acct_id\":31070038925,\"asset_id\":200049395392099,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-04-01 00:00:00\",\"expire_date\":\"2019-04-30 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190430,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049521474514,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-06-01 00:00:00\",\"expire_date\":\"2019-06-30 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190630,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049455735414,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-05-01 00:00:00\",\"expire_date\":\"2019-05-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190531,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049535262245,\"pocket_item\":5920601,\"pocket_name\":\"电商活动和多号专款\",\"amount\":500,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-06-05 20:52:51\",\"expire_date\":\"2019-06-30 23:58:59\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190630,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9990,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049592120473,\"pocket_item\":5920601,\"pocket_name\":\"电商活动和多号专款\",\"amount\":500,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-07-01 00:00:00\",\"expire_date\":\"2019-07-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190731,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9990,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049695025140,\"pocket_item\":5920601,\"pocket_name\":\"电商活动和多号专款\",\"amount\":500,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-08-01 00:00:00\",\"expire_date\":\"2019-08-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190831,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9990,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200048899854687,\"pocket_item\":5037013,\"pocket_name\":\"集团移动商城充值折扣赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":0,\"allow_refund\":1,\"priority\":8998,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200048899854688,\"pocket_item\":5037012,\"pocket_name\":\"移动商城现金支付\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":0,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":6999,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200048904991559,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2018-09-01 00:00:00\",\"expire_date\":\"2018-09-30 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20180930,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200048902149505,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"2018-08-31 14:59:30\",\"expire_date\":\"2099-12-31 23:58:59\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200050184908817,\"pocket_item\":5037014,\"pocket_name\":\"总对总天猫充值预存\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":0,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":6999,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200048957692833,\"pocket_item\":5901432,\"pocket_name\":\"营销活动赠款（不可抵扣通信账户支付）\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"2018-09-12 09:14:05\",\"expire_date\":\"2099-12-31 23:58:59\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200048987434787,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2018-10-01 00:00:00\",\"expire_date\":\"2018-10-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20181031,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049038450930,\"pocket_item\":5901432,\"pocket_name\":\"营销活动赠款（不可抵扣通信账户支付）\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"2018-10-17 17:42:11\",\"expire_date\":\"2099-12-31 23:58:59\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049064591835,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2018-11-01 00:00:00\",\"expire_date\":\"2018-11-30 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20181130,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049121770161,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2018-12-01 00:00:00\",\"expire_date\":\"2018-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20181231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200050285060454,\"pocket_item\":5038031,\"pocket_name\":\"集团客户充值宝专项款\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":0,\"allow_refund\":0,\"priority\":7999,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200049196967762,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-01-01 00:00:00\",\"expire_date\":\"2019-01-31 23:59:59\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190131,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049282356637,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-02-01 00:00:00\",\"expire_date\":\"2019-02-28 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190228,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200050300629841,\"pocket_item\":5902151,\"pocket_name\":\"掌厅充值加赠赠费\",\"amount\":500,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":0,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200050300629842,\"pocket_item\":5020011,\"pocket_name\":\"抵扣预存款 \",\"amount\":10000,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":0,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":6795,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200050388388644,\"pocket_item\":5025521,\"pocket_name\":\"全网手机支付缴话费（总对总）预存款\",\"amount\":2663,\"unpay_fee\":0,\"real_fee\":0,\"sts\":1,\"billing_type\":1,\"valid_date\":\"1990-01-01 00:00:00\",\"expire_date\":\"2099-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":1,\"is_reward\":0,\"freeze_value\":0,\"freeze_time\":20991231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":6795,\"itemType\":\"2\"},{\"acct_id\":31070038925,\"asset_id\":200050397258674,\"pocket_item\":5901432,\"pocket_name\":\"营销活动赠款（不可抵扣通信账户支付）\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2020-12-01 00:00:00\",\"expire_date\":\"2020-12-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20201231,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"},{\"acct_id\":31070038925,\"asset_id\":200049337777364,\"pocket_item\":5960002,\"pocket_name\":\"新入网充值送赠费\",\"amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"sts\":0,\"billing_type\":1,\"valid_date\":\"2019-03-01 00:00:00\",\"expire_date\":\"2019-03-31 00:00:00\",\"resource_id\":0,\"is_common_pocket\":0,\"is_reward\":1,\"freeze_value\":0,\"freeze_time\":20190331,\"measure_id\":10403,\"deduct_amount\":0,\"allow_adjust\":1,\"allow_refund\":1,\"priority\":9800,\"itemType\":\"3\"}],\"acct_id\":31070038925,\"amount\":13163,\"expire_amout\":1500,\"unvalid_amount\":0,\"unpay_fee\":0,\"real_fee\":0,\"usable_balance\":12663,\"deposit_balance\":0,\"pay_type\":0,\"oper_time\":\"2021-03-15 15:25:36\",\"user_sts\":0,\"pay_fee\":0,\"balance\":12663,\"real_pro_fee\":0,\"cust_id\":21010065637513,\"user_id\":40200075634383,\"total_late_fee\":0,\"total_usable_balance\":15663},\"errorMsg\":{\"orig_so_nbr\":\"20180917000037275503\",\"so_nbr\":210315111710324,\"finish_date\":\"2021-03-15 15:25:35\",\"result_code\":0}},\"success\":true}";
        //JsonElement jsonEle = new JsonParser().parse(str);
        Map map = new HashMap();
        map.put("acct_id","00000000");
        map.put("asset_id","9999999");
        //System.out.println(json.replaceJsonNode(jsonEle, map).toString());
        //System.out.println(serialize(map));
        String jsonStr = "{\"sOper\":{\"so_nbr\":\"WY2111050040343300\",\"busi_code\":7060,\"so_mode\":38,\"so_date\":\"20220728\",\"charge_flag\":1,\"op_id\":999990101,\"region_code\":21,\"county_code\":21,\"org_id\":0,\"notify_flag\":0},\"req\":{\"test\":{\"name\":\"cz\",\"age\":\"18\"},\"phone_id\":\"19512222841\",\"pay_amount\":99999,\"charge\":1,\"out_so_nbr\":\"1234467777\",\"spec_id\":202020401,\"channel_id\":\"101\",\"bank_id\":84}}";
        String s = anaylizeAmsJson(jsonStr);
        System.out.println(s);

    }
}
